<template>
  <div class="app-container">
    <!-- header区域 -->
    <mt-header fixed title="黑马程序员">
      <span slot="left" @click="goBack" v-if="flag">
        <mt-button icon="back">返回</mt-button>
      </span>
    </mt-header>
    <transition>
      <router-view></router-view>
    </transition>

  <!-- 底部 -->
  <nav class="mui-bar mui-bar-tab">
			<router-link class="mui-tab-item" to="/home">
				<span class="mui-icon mui-icon-home"></span>
				<span class="mui-tab-label">首页</span>
			</router-link>
			<router-link class="mui-tab-item" to="/member">
				<span class="mui-icon mui-icon-contact"></span>
				<span class="mui-tab-label">会员</span>
			</router-link>
			<router-link class="mui-tab-item" to="/cart">
				<span class="mui-icon mui-icon-extra mui-icon-extra-cart"><span class="mui-badge">0</span></span>
				<span class="mui-tab-label">购物车</span>
			</router-link>
			<router-link class="mui-tab-item" to="/search">
				<span class="mui-icon mui-icon-search"></span>
				<span class="mui-tab-label">搜索</span>
			</router-link>
		</nav>
  </div>
</template>


<script>
// 导入加载提示框
import { Indicator } from "mint-ui";
export default {
  data(){
    return {
      flag:false
    }
  },
  created() {
    this.initInterceptors();
  },
  methods: {
    // 初始化拦截器方法
    initInterceptors() {
      this.$http.interceptors.request.use(function(config) {
        // console.log("发起了ajax请求");
        Indicator.open({
          text: "加载中...",
          spinnerType: "fading-circle"
        });
        return config;
      });

      this.$http.interceptors.response.use(function(response) {
        // console.log("ajax请求完毕");
        Indicator.close();
        return response;
      });
    },
    goBack(){
      // console.log(this);
      this.$router.back();
    }
  },
  watch:{
    // 默认情况下页面被强制刷新不会触发watch，但是我们可以让他强制执行一次监视
    "$route.path":{
      handler:function(newVal){
      // if(newVal==='/home'){
      //   this.flag=false
      // }else{
      //   this.flag=true
      // } 
        this.flag=!(newVal==='/home')
      },
     
      immediate:true 
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding-top: 40px;
  padding-bottom: 50px;
  overflow: hidden;
}
.v-enter {
  opacity: 0;
  transform: translateX(100%);
}
.v-leave-to {
  opacity: 0;
  position: absolute;
  transform: translateX(-100%);
}
.v-enter-active,
.v-leave-active {
  transition: all 0.6s ease;
}
</style>
